package com.xauto.pioneer.appuser.activity.takeCar;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.activity.common.TakePhotoActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.Image;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.util.TimeUtil;
import com.xauto.pioneer.appuser.util.ViewUtil;
import com.xauto.pioneer.appuser.view.TopBar;

import org.json.JSONArray;

import butterknife.Bind;
import butterknife.OnClick;

import static com.xauto.pioneer.appuser.activity.common.TakePhotoActivity.DO_BIG_IMAGE;
import static com.xauto.pioneer.appuser.activity.common.TakePhotoActivity.UPLOAD_IMG;

/**取车拍照页面（第一次开车门）
 * Created by lsx on 2018-03-22.
 */

public class TakeCarPhotoOpenActivity extends BaseActivity {
    @Bind(R.id.topBar)
    TopBar topBar;
    @Bind(R.id.tvIdInfoNext)
    TextView tvIdInfoNext;
    @Bind(R.id.Firsr_image)
    ImageView Firsr_image;
    @Bind(R.id.tvIdInfoTakePhoto)
    TextView tvIdInfoTakePhoto;
    //-------------------------
    @Bind(R.id.Second_image)
    ImageView Second_image;
    @Bind(R.id.tvIdInfoTakePhoto_two)
    TextView tvIdInfoTakePhoto_two;
    @Bind(R.id.Threed_image)
    ImageView Threed_image;
    @Bind(R.id.tvIdInfoTakePhoto_three)
    TextView tvIdInfoTakePhoto_three;
    @Bind(R.id.Four_image)
    ImageView Four_image;
    @Bind(R.id.tvIdInfoTakePhoto_four)
    TextView tvIdInfoTakePhoto_four;
    /** 当前上传照片的服务器路径**/
    private String currentUpdatePath = "";
    public ImageLoader imageLoader;
    Uri imageUri;
    //车辆编号
    String carNo = "";
    String longitude = "", latitude = "", address = "";
    //设定拍照对象
    int Witch=0;
    int OnePc=0;
    int TwoPc=0;
    int ThreePc=0;
    int FourPc=0;
    String Pauth1="";
    String Pauth2="";
    String Pauth3="";
    String Pauth4="";
    String localPauth1="";
    String localPauth2="";
    String localPauth3="";
    String localPauth4="";
    //判断什么界面
    String status="0";
    String ImageMessage="时间:"+ TimeUtil.getFormatTime()+"地点:"+""+"经纬度:";
    @Override
    protected int getContentView() {
        return R.layout.ac_take_car;
    }

    @Override
    protected void initView() {
        tvIdInfoNext.setClickable(false);
        topBar.showView(TopBar.TITLE, TopBar.LEFT_IMAGE_BTN);
        topBar.setText(TopBar.TITLE, "取车拍照");
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void initData() {
        carNo = getIntent().getStringExtra("car_no");
        longitude = getIntent().getStringExtra("longitude");
        latitude = getIntent().getStringExtra("latitude");
        address = getIntent().getStringExtra("address");
    }

    @OnClick({R.id.tvIdInfoNext,R.id. Firsr_image, R.id.Second_image,R.id.Threed_image,R.id.Four_image,
    R.id.tvIdInfoTakePhoto,R.id.tvIdInfoTakePhoto_two,R.id.tvIdInfoTakePhoto_three,R.id.tvIdInfoTakePhoto_four})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvIdInfoNext:
                    preReturnCar(Pauth1,Pauth2,Pauth3,Pauth4);
                break;
            case R.id.Firsr_image:
                if(OnePc ==1){
                    ViewUtil.showBigPic(this,localPauth1);
                }
                break;
            case R.id.Second_image:
                if(TwoPc ==1){
                    ViewUtil.showBigPic(this,localPauth2);
                }
                break;
            case R.id.Threed_image:
                if(ThreePc ==1){
                    ViewUtil.showBigPic(this,localPauth3);
                }
                break;
            case R.id.Four_image:
                if(FourPc ==1){
                    ViewUtil.showBigPic(this,localPauth4);
                }
                break;
            case R.id.tvIdInfoTakePhoto:
                startActivityForResult(TakePhotoActivity.getTakePhotoStartIntent(mContext,
                        DO_BIG_IMAGE, 1, 1, 1080, 1080), UPLOAD_IMG);
                Witch=1;
                break;
            case R.id.tvIdInfoTakePhoto_two:
                startActivityForResult(TakePhotoActivity.getTakePhotoStartIntent(mContext,
                        DO_BIG_IMAGE, 1, 1,1080, 1080), UPLOAD_IMG);
                Witch=2;
                break;
            case R.id.tvIdInfoTakePhoto_three:
                startActivityForResult(TakePhotoActivity.getTakePhotoStartIntent(mContext,
                        DO_BIG_IMAGE, 1, 1,1080, 1080), UPLOAD_IMG);
                Witch=3;
                break;
            case R.id.tvIdInfoTakePhoto_four:
                startActivityForResult(TakePhotoActivity.getTakePhotoStartIntent(mContext,
                        DO_BIG_IMAGE, 1, 1,1080, 1080), UPLOAD_IMG);
                Witch=4;
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == TakePhotoActivity.UPLOAD_SUCCESS) {
            if (data != null && data.getExtras() != null) {
                currentUpdatePath=data.getExtras().getString("PATH");
                Bitmap bmp= BitmapFactory.decodeFile(data.getExtras().getString("PATH"));
                if(Witch==1){
                    localPauth1 = currentUpdatePath;
                    loadEditImage(currentUpdatePath,Constants.IMAGE_ORDER_BEFORE);
                    Firsr_image.setImageBitmap(bmp);
                    tvIdInfoTakePhoto.setText("点击重新拍照");
                    OnePc=1;}
                if(Witch==2){
                    localPauth2 = currentUpdatePath;
                    loadEditImage(currentUpdatePath,Constants.IMAGE_ORDER_BEFORE);
                    Second_image.setImageBitmap(bmp);
                    tvIdInfoTakePhoto_two.setText("点击重新拍照");
                    TwoPc=1;}
                if(Witch==3){
                    localPauth3 = currentUpdatePath;
                    loadEditImage(currentUpdatePath,Constants.IMAGE_ORDER_BEFORE);
                    Threed_image.setImageBitmap(bmp);
                    tvIdInfoTakePhoto_three.setText("点击重新拍照");
                    ThreePc=1;}
                if(Witch==4){
                    localPauth4 = currentUpdatePath;
                    loadEditImage(currentUpdatePath,Constants.IMAGE_ORDER_FEEDBACK);
                    Four_image.setImageBitmap(bmp);
                    tvIdInfoTakePhoto_four.setText("点击重新拍照");
                    FourPc=1;}
                if(OnePc==1&&TwoPc==1&&ThreePc==1&&FourPc==1){
                    tvIdInfoNext.setClickable(true);
                    ViewUtil.GetViewShgape(tvIdInfoNext);
                    tvIdInfoNext.setBackgroundResource(R.drawable.bg_btn_green_selector);
                    ViewUtil.SetViewShgape(tvIdInfoNext);
                }
            }
        }
    }
    private void loadEditImage(String path,String TYPE) {
        new LHttpLib().editImage(mContext, memberId, TYPE, path, new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                final Image image = new Image().parse(jsonStatus.data.optJSONObject("image_info"));
                currentUpdatePath = image.source;
                if(Witch==1){
                    Pauth1=image.source;}
                if(Witch==2){
                    Pauth2=image.source;}
                if(Witch==3){
                    Pauth3=image.source;}
                if(Witch==4){
                    Pauth4=image.source;}
            }
        });
    }
    /**
     * 用车前牌照
     * */
    private void preReturnCar(String Pauth1,String Pauth2,String Pauth3,String Pauth4) {
        JSONArray arry=new JSONArray();
        arry.put(Pauth1); arry.put(Pauth2); arry.put(Pauth3); arry.put(Pauth4);
        new LHttpLib().getPreReturnCar(mContext, memberId, carNo, arry, new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        FoundCarActivity.Order=1;
                        FoundCarActivity.OrderPhoto=1;
                        finish();
                    }
                });
            }
        });
    }
}
